package com.yanqu.road.dao.impl.activity.pawnshop;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pawnshop.UserPawnshop;
import com.yanqu.road.entity.activity.pawnshop.UserPawnshopStaff;
import com.yanqu.road.utils.string.StringUtils;

public class UserPawnshopDaoImpl extends TempDataObjectDao<UserPawnshop> {

	public static String getUserPawnshopSql = "select * from `t_u_activity_pawnshop` where `userId`=? and `activityId`=?";

	public UserPawnshop getUserPawnshop(long userId, int activityId) {
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserPawnshopSql);
			int i = 1;
			call.setLong(i++, userId);
			call.setInt(i++, activityId);
			rs = call.executeQuery();
			if (rs.next()) {
				return parse(rs);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public static String updateUserPawnshopSql = "update `t_u_activity_pawnshop` set receptionNum=?,level=?,coinUsable=?,washTimes=?," +
			"staminaUsable=?,lastRestoreTime=?,staffData=?,politicsSn=?,politicsAddition=?,politicsStartReceptionNum=?," +
			"politicsTriggerRate=?,politicsRead=?,npcIcon = ?,isReception = ?,lvReward = ? where userId=? and activityId=?";

	@Override
	protected boolean updateData(UserPawnshop userPawnshop) {
		userPawnshop.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getReceptionNum()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getLevel()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshop.getCoinUsable()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getWashTimes()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getStaminaUsable()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshop.getLastRestoreTime()));
		para.put(i++, new DbParameter(Types.VARCHAR, userPawnshop.getStaffMapJsonStr()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getPoliticsEventId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getPoliticsAddition()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getPoliticsStartReceptionNum()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getPoliticsTriggerRate()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshop.isPoliticsRead()));
		para.put(i++, new DbParameter(Types.NVARCHAR, userPawnshop.getNpcIcon()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshop.isReception()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getLvReward()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshop.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getActivityId()));
		if (getDb().executeNoneQuery(updateUserPawnshopSql, para) > 0) {
			return true;
		}
		return false;
	}

	public static String insertUserPawnshopSql = "insert into `t_u_activity_pawnshop`(userId,activityId,receptionNum,level,coinUsable," +
			"washTimes,staminaUsable,lastRestoreTime,staffData,politicsSn,politicsAddition,politicsStartReceptionNum,politicsTriggerRate," +
			"politicsRead,npcIcon,isReception,lvReward) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	@Override
	protected boolean addData(UserPawnshop userPawnshop) {
		userPawnshop.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshop.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getActivityId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getReceptionNum()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getLevel()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshop.getCoinUsable()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getWashTimes()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getStaminaUsable()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshop.getLastRestoreTime()));
		para.put(i++, new DbParameter(Types.VARCHAR, userPawnshop.getStaffMapJsonStr()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getPoliticsEventId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getPoliticsAddition()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getPoliticsStartReceptionNum()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getPoliticsTriggerRate()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshop.isPoliticsRead()));
		para.put(i++, new DbParameter(Types.NVARCHAR, userPawnshop.getNpcIcon()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshop.isReception()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshop.getLvReward()));
		if (getDb().executeNoneQuery(insertUserPawnshopSql, para) > 0) {
			return true;
		}
		return false;

	}

	@Override
	protected UserPawnshop parseData(ResultSet rs) throws SQLException {
		UserPawnshop data = new UserPawnshop();
		data.setUserId(rs.getLong("userId"));
		data.setActivityId(rs.getInt("activityId"));
		data.setReceptionNum(rs.getInt("receptionNum"));
		data.setLevel(rs.getInt("level"));
		data.setCoinUsable(rs.getLong("coinUsable"));
		data.setWashTimes(rs.getInt("washTimes"));
		data.setStaminaUsable(rs.getInt("staminaUsable"));
		data.setLastRestoreTime(rs.getLong("lastRestoreTime"));
		String value = rs.getString("staffData");
		if(!StringUtils.isNullOrEmpty(value)){
			Map<Integer, UserPawnshopStaff> staffMap = JSON.parseObject(value, new TypeReference<Map<Integer, UserPawnshopStaff>>() {});
			data.setStaffMap(staffMap);
		}
		data.setPoliticsEventId(rs.getInt("politicsSn"));
		data.setPoliticsAddition(rs.getInt("politicsAddition"));
		data.setPoliticsStartReceptionNum(rs.getInt("politicsStartReceptionNum"));
		data.setPoliticsTriggerRate(rs.getInt("politicsTriggerRate"));
		data.setPoliticsRead(rs.getBoolean("politicsRead"));
		data.setNpcIcon(rs.getString("npcIcon"));
		data.setReception(rs.getBoolean("isReception"));
		data.setLvReward(rs.getInt("lvReward"));
		return data;
	}

}
